package com.aimao.blog.service.impl;

import com.aimao.blog.constant.ServerResponse;
import com.aimao.blog.dao.BannerMapper;
import com.aimao.blog.pojo.Banner;
import com.aimao.blog.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author Jack Chen
 * @Create 2019-01-30 10:57
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public ServerResponse selectBannerList() {
        return ServerResponse.createSuccessAndData(bannerMapper.selectAllList());
    }

    @Override
    public ServerResponse getBannerById(Integer id) {
        Banner banner = bannerMapper.selectByPrimaryKey(id);
        if (banner == null) {
            return ServerResponse.createFailure("没有这条记录");
        }
        return ServerResponse.createSuccessAndData(banner);
    }

    @Override
    public ServerResponse deleteBannerById(Integer id) {
        int row = bannerMapper.deleteByPrimaryKey(id);
        return row <= 0 ? ServerResponse.createFailure("删除失败") : ServerResponse.createSuccessAndData("删除成功");
    }

    @Override
    public ServerResponse updateBannerById(Banner banner) {
        int row = bannerMapper.updateByPrimaryKeySelective(banner);
        return row <= 0 ? ServerResponse.createFailure("更新失败") : ServerResponse.createSuccessAndData("更新成功");
    }

}
